<?php 
/*
说明 : 这是注册 ajax 请求文件
*/
include '../../configs/global.php';
$data=$_POST;

$username=$data['name'];
$password=$data['pass'];
$repassword=$data['repass'];
$sid=$data['sid'];
$sex=$data['sex'];

// 定义一个返回数组
$ajax_res=array();

// 判断是否为空
if(!$username || !$password || !$repassword  || !$sid || !sex ){
	$ajax_res['code']=300;
	$ajax_res['info']='填写的信息不完善';
	echo json_encode($ajax_res);
	exit;
}

$sql="select * from student where name='$username'";

$row=$pdo->getOne($sql);

if(!$row){
	$password=md5($password);
	$sql="insert into student(name,password,sid,sex) values('{$username}','{$password}','{$sid}','{$sex}')";
	$r1=$pdo->exec($sql);
	if($r1){
		$sql="select * from student where name='{$username}'";
		$r=$pdo->getOne($sql);
		// 添加 session 标识，用于判断是否登录
        $_SESSION['username'] = $username;
		$ajax_res['code']=200;
		$ajax_res['info']='注册成功';
	}else{
		$ajax_res['code']=300;
		$ajax_res['info']='注册失败,请稍后再试！';
	}
}else{
	$ajax_res['code']=300;
	$ajax_res['info']='用户已存在';
}
echo json_encode($ajax_res);
 ?>